﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using ABCat.Core;
using ABCat.Core.Plugins.DataSets;
using ABCat.Core.Plugins.Sites;
using ABCat.Shared;

namespace ABCat.UI.WPF.Models
{
	public class SiteParserViewModel : AsyncOperationViewModelBase
	{
		private readonly ABCatViewModel _owner;
		private readonly Func<IEnumerable<IAudioBook>> _getSelectedItems;
		private readonly DelegateCommand _reparseCommand;
		private readonly DelegateCommand _downloadRecordsCommand;
		private readonly DelegateCommand _downloadRecordGroupsCoomand;

		#region + Properties +


		public DelegateCommand ReparseCommand
		{
			get
			{
				return _reparseCommand;
			}
		}

		private void Reparse(object obj)
		{
			CancellationTokenSource = new CancellationTokenSource();

			SiteParserPlugin.BeginDownloadRecordsAsync(new HashSet<string>(_getSelectedItems().Select(item => item.Key).Distinct()),
				true,
				ReportProgressSmall,
				ReportProgressTotal,
				DownloadRecordsAsyncCompleted,
				CancellationTokenSource.Token);
		}

		private bool IsCanReparse(object arg)
		{
			return CancellationTokenSource == null;
		}

		public DelegateCommand DownloadRecordGroupsCommand
		{
			get
			{
				return _downloadRecordGroupsCoomand;
			}
		}

		public DelegateCommand DownloadRecordsCommand
		{
			get
			{
				return _downloadRecordsCommand;
			}
		}

		public ISiteParserPlugin SiteParserPlugin { get; set; }

		#endregion
		#region + Ctor +

		public SiteParserViewModel(ABCatViewModel owner, ISiteParserPlugin siteParserPlugin, Func<IEnumerable<IAudioBook>> getSelectedItems)
			: base(owner.StatusBarStateModel)
		{
			_owner = owner;
			_getSelectedItems = getSelectedItems;
			SiteParserPlugin = siteParserPlugin;
			_reparseCommand = new DelegateCommand(Reparse, IsCanReparse);
			_downloadRecordsCommand = new DelegateCommand(OnDownloadRecords, CanDownloadRecords);
			_downloadRecordGroupsCoomand = new DelegateCommand(OnDownloadRecordGroups, CanDownloadRecordGroups);
		}

		#endregion
		#region + Logic +

		public bool CanDownloadRecordGroups(object parameter)
		{
			return CancellationTokenSource == null;
		}

		public bool CanDownloadRecords(object parameter)
		{
			return CancellationTokenSource == null;
		}

		public void OnDownloadRecordGroups(object parameter)
		{
			CancellationTokenSource = new CancellationTokenSource();

			SiteParserPlugin.BeginDownloadRecordGroupsAsync(null,
				ReportProgressSmall,
				ReportProgressTotal,
				DownloadRecordGroupsAsyncCompleted,
				CancellationTokenSource.Token);
		}

		public void OnDownloadRecords(object parameter)
		{
			CancellationTokenSource = new CancellationTokenSource();

			SiteParserPlugin.BeginDownloadRecordsAsync(null,
				false,
				ReportProgressSmall,
				ReportProgressTotal,
				DownloadRecordsAsyncCompleted,
				CancellationTokenSource.Token);
		}

		private void DownloadRecordGroupsAsyncCompleted(Exception obj)
		{
			_owner.Context.UiDispatcher.CheckAccess(() =>
				{
					if (obj == null)
					{
						OnAsynOperationCompleted();
						_owner.RefreshRecordsListData();
					}
				});
		}

		private void DownloadRecordsAsyncCompleted(Exception obj)
		{
			_owner.Context.UiDispatcher.CheckAccess(() =>
				{
					if (obj == null)
					{
						OnAsynOperationCompleted();
						_owner.RefreshRecordsListData();
					}
				});
		}

		#endregion
	}
}